import { Component, OnInit, OnDestroy, EventEmitter, Output, ViewChild, Input } from '@angular/core';
import { NzTreeNodeOptions, NzTreeNode, NzTreeComponent, NzFormatEmitEvent } from 'ng-zorro-antd';
import { Subscription } from 'rxjs';
import { legendsData } from './legends-data';
import { ToolsService } from 'app/shared/service';
import { DataManagementService } from '../../../pages/data-management/data-management.services';

@Component({
    selector: 'itmp-legends',
    templateUrl: './legends.component.html',
    styleUrls: ['./legends.component.css']
})
export class LegendsComponent implements OnInit, OnDestroy {
    @ViewChild('nzTreeComponent') treeCom: NzTreeComponent;
    @Output() nodeSelctChange = new EventEmitter<any>();
    @Output() private treeDataList = new EventEmitter<string>();
    // @Input() zoneAssignment
    zoneAssignment = [];
    // treeData: NzTreeNodeOptions[] = this.tools.isClone(legendsData);
    treeData: NzTreeNodeOptions[] = this.tools.isClone(legendsData);
    subscriptions: Array<Subscription> = [];
    constructor(private tools: ToolsService, private DMService: DataManagementService) {}
    select(event: NzFormatEmitEvent) {
        this.nodeSelctChange.emit(event['node'] as NzTreeNode);
    }
    getAllZoneList() {
        this.DMService.getAllEnumInfo().subscribe(res => {
            if (res.code === '000000') {
                const data = [];
                res.data.sort((a, b) => a.value - b.value);
                res.data.forEach(ele => {
                    if (ele.category === 'zone') {
                        ele.title = ele.name;
                        ele.key = '3-' + ele.value;
                        ele.isLeaf = true;
                        ele.layerCategory = 'ZONE';
                        ele.typeCode = JSON.parse(ele.value);
                        data.push(ele);
                    }
                });
                legendsData[3].children = data;
                this.zoneAssignment = legendsData;
                this.treeData = this.tools.isClone(this.zoneAssignment);
                this.treeDataList.emit(JSON.stringify(this.treeData)); // 广播传递数据给父组件
            }
        });
    }
    ngOnInit() {
        this.getAllZoneList();
    }
    ngOnDestroy(): void {
        for (const subscription of this.subscriptions) {
            if (subscription) {
                subscription.unsubscribe();
            }
        }
    }
}
